int add_device_to_mount_table(int deviceHandle, char* deviceName, MINODE* mntPnt)
{
	int i, mountEntry;
	char mountPath[MAX_PATH_LENGTH];
	EXT2_DISK diskInfo;
	SUPER super;
		
	if (get_super(deviceHandle, &super) == -1)
		return -1;
	
	diskInfo.blockSize = STD_BLK_SIZE;
	for (i = 0; i < super.s_log_block_size; i++)
		diskInfo.blockSize *= 2;
	diskInfo.inodeSize = super.s_inode_size;
	diskInfo.numInodes = super.s_inodes_count;
	diskInfo.numBlocks = super.s_blocks_count;
	diskInfo.inodesPerBlk = diskInfo.blockSize / diskInfo.inodeSize;
	diskInfo.groupsPerBlk = diskInfo.blockSize / sizeof(GROUP);
	diskInfo.numGroups = diskInfo.numInodes / super.s_inodes_per_group;
	
	mountEntry = find_free_mount();
	if (mountEntry == -1)
	{
		printf("Mount table full!\n");
		return -1;
	}

	mountTable[mountEntry].mountedInode = mntPnt;
	mountTable[mountEntry].diskInfo = diskInfo;
	mountTable[mountEntry].busy = true;
	mountTable[mountEntry].handle = deviceHandle;
	
	if (mntPnt != 0)
		get_pwd_name(mntPnt, mountPath);

	strcpy(mountTable[mountEntry].diskInfo.name, deviceName);
	strcpy(mountTable[mountEntry].mountedName, mountPath);
	
	if (mntPnt != 0)
	{
		mntPnt->mounted = true;
		mntPnt->mountedDevice = &mountTable[mountEntry];
	}
	
	return mountEntry;
}

int mount_device(char* fsPath, char* mountPath)
{
	int i, handle;
	LOC mountLoc;
	MINODE* mountPnt = 0;
	
	for (i = 0; i < NUM_MOUNTS; i++)
	{
		if ((mountTable[i].mountedInode != 0) &&
			(strcmp(mountTable[i].diskInfo.name, fsPath) == 0))
		{
			printf("\"%s\" already mounted\n\n", fsPath);
			return -1;
		}
	}
	
	mountLoc = get_inode_number(mountPath);
	if (mountLoc.inodeNum == 0)
	{
		printf("\"%s\" does not exist\n\n", mountPath);
		return -1;
	}
	
	mountPnt = get_inode(mountLoc.deviceHandle, mountLoc.inodeNum);
	if (mountPnt == 0)
		return -1;
		
	if (S_ISDIR(mountPnt->inode.i_mode))
	{
		char buf[STD_BLK_SIZE];
		EXT2_DISK diskInfo;
		
		for (i = 0; i < NUM_PROCS; i++)
		{
			if (procTable[i].cwd == mountPnt)
			{
				printf("\"%s\" is busy\n\n", mountPath);
				return -1;
			}
		}
				
		handle = open64(fsPath, O_RDWR);
		if (handle < 0)
		{
			printf("Unable to open \"%s\"\n\n", fsPath);
			put_inode(mountPnt);
			return -1;
		}
		
		if (read_blk(handle, 1, STD_BLK_SIZE, buf) == -1)
		{
			put_inode(mountPnt);
			return -1;
		}
			
		if (((SUPER*)buf)->s_magic != EXT2_MAGIC)
		{
			printf("The specified device does not contain a recognized Ext2/Ext3 filesystem\n\n");
			put_inode(mountPnt);
			return -1;
		}
		
		if (((SUPER*)buf)->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)
			printf("Mounted filesystem is EXT3\n\n");
		else
			printf("Mounted filesystem is EXT2\n\n");
			
		if (add_device_to_mount_table(handle, fsPath, mountPnt) == -1)
		{
			printf("Unable to add device to mount table\n\n");
			put_inode(mountPnt);
			return -1;
		}
		
		return 1;
	}
	
	printf("\"%s\" is not a directory\n\n", mountPath);
	put_inode(mountPnt);
	return -1;
}

int do_mount(char* args)
{
	char* fs, *path;
	char fullFsPath[MAX_PATH_LENGTH];
	char fullPath[MAX_PATH_LENGTH];
	
	if (strlen(args) == 0)
	{
		int i;
		for (i = 0; i < NUM_MOUNTS; i++)
		{
			if (mountTable[i].mountedInode != 0)
			{
				printf("\"%s\" mounted on \"%s\"\n", mountTable[i].diskInfo.name,
					mountTable[i].mountedName);
			}
		}
		putchar('\n');
	}
	else
	{	
		if (strchr(args, ' ') == 0)
		{
			printf("Not enough args\n");
			return -1;
		}
		
		fs = strtok(args, " ");
		path = strtok(0, " \n");
		
		if (fs[0] != '/')
		{
			char tmp[MAX_PATH_LENGTH];
			get_pwd_name(runningProc->cwd, tmp);
			strcat(tmp, fs);
			strcpy(fullFsPath, tmp);
		}
		else
			strcpy(fullFsPath, fs);
			
		if (path[0] != '/')
		{
			char tmp[MAX_PATH_LENGTH];
			get_pwd_name(runningProc->cwd, tmp);
			strcat(tmp, path);
			strcpy(fullPath, tmp);
		}
		else
			strcpy(fullPath, path);
		
		return mount_device(fullFsPath, fullPath);
	}
}
